<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/core" prefix="c"%>
<%@ taglib uri="/tags/fmt" prefix="fmt"%>

<fmt:setBundle basename="messages"/>

<html>
	<head>
		<title>Index File</title>
		<LINK REL="StyleSheet" HREF="css/style.css" TYPE="text/css"/>
		<LINK REL="StyleSheet" HREF="css/principal.css" TYPE="text/css" />
		<script type="text/javascript" src="js/calendar/PopupWindow.js"></script>
		<script type="text/javascript" src="js/calendar/CalendarPopup.js"></script>
		<script type="text/javascript" src="js/calendar/AnchorPosition.js"></script>
		<script type="text/javascript" src="js/calendar/date.js"></script>
		<script type="text/javascript">  

	function validateForm(){
	    	var errorLen  = '';
	    	var prods = document.getElementById("prods");
	    	if(prods!=null && prods.value > 0){
	    		for(i = 0; i< prods.value; i = i+1 ){
	    			var qp = document.getElementById("qp" + i )
	    			if((qp.value == "") ||  (qp.value == "0")){
	    				errorLen = errorLen + '<bean:message key="error.message.futurepurchase.enter.amount.for.all.products"/>\n';
	    				break;
	    			}

	    			if(!(qp.value == "") && (isNaN(qp.value))){
	    				errorLen = errorLen + '<bean:message key="error.message.futurepurchase.enter.amount.for.all.products"/>\n';
	    				break;
	    			}	    	
	    		}
	    	}
	    	return errorLen;
		}

		function submitForm(action){			
	    	var form = document.getElementById('mainForm');	    	
	    	form.action = action;
	    		    
	    	if(action=='fpaSaveAssignProducts.do'){
	    		var errors = validateForm();	    	
	    		if( errors == '' ){
				    form.submit();
				}else{
					alert(errors);
				}				
			}else{
				form.submit()
			}
		}
	
		function back(action){			
	    	var form = document.getElementById('mainForm');	    	
	    	form.action = action;
	    	form.submit();
		}
		</script>	
	</head>
	<DIV ID="div_fechaNacimiento" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
	<body>
		<html:form action="/fpaCustomerAssignProducts" styleId="mainForm">
			<label class="etiqueta_campo"><bean:message key="message.futurepurchase.customer"/>:
			<bean:write name="FuturePurchaseForm" property="futurePurchaseView.customerPurchaseSelected.customerId" />&nbsp;<bean:write name="FuturePurchaseForm" property="futurePurchaseView.customerPurchaseSelected.description" />
			</label>
			<br/>
			<br/>
			<table style="border-style:solid; border-width:1px; border-color: #cacaca" >
				<tr>
					<td colspan="3">
					
						<table border="0">
							<tr>
								<td class="campo_texto"><bean:message key="message.futurepurchase.product.search"/><br/><html:text property="productViewSearch.description" styleClass="campo_texto"/></td>
								<td>&nbsp;<br/><input type="button" value="..." onclick="javascript:submitForm('fpaProductSearch.do')" class="boton_short"/></td>
							</tr>
						</table>		
					
					</td>
				</tr>
				<tr>
					<td valign="top">
						<table style="border-style:solid; border-width:1px; border-color: #cacaca" cellspacing="1" >
						    <tr class="titulocolumna" bgcolor="#7A95FF">
						      <td width="10px" >&nbsp;</td>	
						      <td width="60px"><bean:message key="message.futurepurchase.customer.code"/></td>
						      <td width="60px"><bean:message key="message.futurepurchase.customer.name"/></td>
						    </tr>   
							<c:forEach items="${FuturePurchaseForm.resultProducts}" var="product" >
								<tr>								
								  	<td width="10px">
								  	<input type="checkbox" value='<c:out value="${product.productId}"/>' name="futurePurchaseView.customerPurchaseSelected.selectedProductIds"></td>
								  	<td width="60px" class="columnadato"><c:out value="${product.productId}"/></td>
								  	<td width="60px" align="left" class="columnadato"><c:out value="${product.description}"/></td>


								</tr>
							</c:forEach>
						</table>

					</td>
					<td height="100%" valign="top">

						<table >
							<tr>
								<td >
									<input type="button" value='<bean:message key="message.futurepurchase.add"/>' style="width: 80px" class="boton"
									onclick="javascript:submitForm('fpaProductToCustomerAdd.do')" />

								</td>
							</tr>
							<tr style="text-align: center;">
								<td>
									<input type="button" style="width: 80px" value='<bean:message key="message.futurepurchase.remove"/>' class="boton"
									onclick="javascript:submitForm('fpaProductToCustomerRemove.do')"/>
								</td>
							</tr>
						</table>

					</td>
					<td valign="top">
						<table style="border-style:solid; border-width:1px; border-color: #cacaca">
							<tr class="titulocolumna" bgcolor="#7A95FF">
								<td></td>
								<td><bean:message key="message.futurepurchase.customer.code"/></td>
								<td><bean:message key="message.futurepurchase.customer.name"/></td>
								<td><bean:message key="message.futurepurchase.ton"/></td>							
							</tr>							
								<c:forEach var="productPurchase" items="${FuturePurchaseForm.futurePurchaseView.customerPurchaseSelected.productPurchaseViews}" varStatus="loop" >
									<tr>
									<td>
									<input type="checkbox" value='<c:out value="${productPurchase.productId}"/>' id='sc<c:out value="${loop.index}"/>' name="futurePurchaseView.customerPurchaseSelected.removedProductIds"/>

									</td>
									<td class="columnadato">
								  	<bean:write name="productPurchase" property="productId" />
								  	</td>
									<td class="columnadato">
								  	<bean:write name="productPurchase" property="description" />
								  	</td>
									<td  class="columnaCaptura">
									  <input value='<fmt:formatNumber pattern="###,####0.000" type="number" maxFractionDigits="4" value="${productPurchase.quantity}"/>'
									  class="campo_texto"
									  onkeyup="extractNumber(this,3,false);"
									  onfocus="this.select();" 
									  name="futurePurchaseView.customerPurchaseSelected.productPurchaseViews[<c:out value="${loop.index}"/>].quantity" 									  
									  onkeydown="return (numeric(event, 1) && noPaste(event, this));"
									  style="text-align:right"
									  id='qp<c:out value="${loop.index}"/>'/>
					

								  	</td>									
								  	</tr>
								  
								</c:forEach>

						</table>					

					</td>
				</tr>							
			</table>
			<br/>
			<table style="border-style:solid; border-width:1px; border-color: #cacaca" cellspacing="1">
			<tr >
					<td colspan="3" align="center">
					<html:button property="accept"  onclick="javascript:submitForm('fpaSaveAssignProducts.do')" styleClass="boton"><bean:message key="label.credit.line.save"/></html:button>
					
					<td colspan="3" align="centger">
					<!-- <input type="button" value="Regresar" onclick="location.href='fpaCloseChildWindow.do?cancel=true'" styleClass="boton" /> -->
					<html:button property="cancel"  onclick="location.href='fpaCancelAssignProducts.do'" styleClass="boton"><fmt:message key="label.common.cancel"/></html:button>
					<input type='hidden' value='<c:out value="${FuturePurchaseForm.futurePurchaseView.customerPurchaseSelected.productPurchaseViewsSize}"/>' id='prods' />
					</td>
				
					<!-- <input type="button" value="Aceptar" onclick="javascript:submitForm('fpaCloseChildWindow.do')" styleClass="boton"/> -->
					</td>
				</tr>	
			</table>
		</html:form>
	</body>
	
</html>